home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / system / idrv01.zip / IDRV.ASM < prev    next >
Assembly Source File  |  1991-11-25  |  26KB  |  1,042 lines

  1.  
  2. NAME    Device_Driver_Installer
  3. TITLE    Idrv.asm
  4. ;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::;
  5. ;                              18-11-1991           ;
  6. ;    Idrv.asm (v1.06)                                   ;
  7. ;    compiled with Turbo Assembler v1.0                           ;
  8. ;                        by Janusz Wojcik            ;
  9. ;                        Lublin,Poland.               ;
  10. ;..............................................................................;
  11.  
  12.  
  13.         ASSUME DS:st_seg, SS:st_seg ,CS:st_seg ,ES:st_seg
  14. st_seg        SEGMENT    
  15.         ORG    100h
  16.  
  17. start:        
  18.         mov    ax,es
  19.         dec    ax
  20.         mov    es,ax            ;ES = ES - 1
  21.         mov    ax,word ptr es:3    ;get size of memory block from MCB
  22.         push    ax
  23.         mov    bx,es
  24.         add    ax,bx            ;compute address of the end of memory block
  25.         mov    bx,LEN + 15        ;compute size of the program ...
  26.         mov    cl,4
  27.         shr    bx,cl            ; ... in paragraphs
  28.         sub    ax,bx            ;calculate new address of program's beginning
  29.  
  30.         mov    jmp_seg,ax        ;save it
  31.         mov    es,ax
  32.             ;.......................................................
  33.         pop    ax
  34.         mov    dx,10h                 ;add PSP size
  35.         add    dx,bx
  36.         sub    ax,dx
  37.         cmp    ax,1000h        ;some drivers set stack at the ...
  38.                         ; ... end of the segment !!!
  39.         ja    move_code        ;jump if more than 64KB left
  40.         lea    dx,too_small
  41.         mov    ah,9
  42.         int    21h
  43.  
  44.         mov    ah,4ch        ;EXIT
  45.         mov    al,1        ;Set return code
  46.         int    21h
  47.             ;.......................................................
  48. move_code:
  49.         mov    cx,LEN
  50.         mov    di,0
  51.         mov    si,offset pr_end
  52.         cld
  53.         rep    movsb            ;move program into upper memory
  54.     
  55.         jmp    dword ptr jmp_offs        ;and go there
  56.  
  57. too_small    db    'IDrv: Too small memory!',cr,lf,lf,'$'
  58.  
  59. jmp_offs    dw    0
  60. jmp_seg        dw    0
  61.  
  62. ALIGN        16
  63. pr_end        equ    $
  64.  
  65. st_seg        ends
  66. ;*******************************************************************************
  67.  
  68.         ASSUME DS:code, SS:code ,CS:code ,ES:code
  69. code        SEGMENT    Byte Public 'code'
  70.     
  71. new_seg        equ    $
  72.         mov    ax,cs
  73.         cli
  74.         mov    ss,ax            ;Set own stack
  75.         mov    sp,offset EOS
  76.         sti
  77.  
  78.         mov    ax,ds
  79.         push    cs
  80.         pop    ds            ;DS = CS
  81.         mov    our_seg,ax
  82.  
  83.         jmp    starter        ; jump over data
  84. own_stack    db    157 dup(0)
  85. EOS        equ    $        ; End Of Stack
  86.  
  87.  
  88. ;...............................................................................
  89.  
  90. device_header    struc
  91. next_offs    dw    ?
  92. next_seg    dw    ?
  93. dev_attr    dw    ?
  94. dev_strategy    dw    ?
  95. dev_interrupt    dw    ?
  96. name_unit    db    8 dup(?)
  97. device_header    ends
  98.  
  99. ;Idrv Control Block
  100. icb        struc
  101. signature    db    7 dup(?)
  102. xms_marker    db    3 dup(?)
  103. prv_xms_offs    dw    ?
  104. prv_xms_seg    dw    ?
  105. xms_handle    dw    ?
  106. icb        ends
  107. .errnz        16 - SIZE icb
  108. ;...............................................................................
  109.  
  110. lf        equ    0ah        ; line feed
  111. cr        equ    0dh        ; carriage return
  112.  
  113. ZERO        equ    0
  114. INIT        equ    0
  115. TAB        equ    9
  116.  
  117. DIB_SIZE    equ    21h
  118. FIRST_DIB    equ    26h
  119. FIRST_DA    equ    3ch
  120. NUM_OF_DRV    equ    46h
  121. TOTAL_NUM    equ    47h
  122. MAX_SECTOR    equ    36h
  123. JMP_SHORT    equ    3ebh
  124. JMP_FAR        equ    0eah
  125.  
  126. ;...............................................................................
  127. ;    the program body
  128. ;..............................................................................        
  129. ldrv:  
  130.         call    load_drv
  131.         jnc    idev
  132.         jmp    exit
  133.  
  134. idev:        
  135.         mov    es,dos_seg
  136.         mov    al,byte ptr es:NUM_OF_DRV    ;Get real number of drives
  137.         mov     drive_num,al        ;Set unit number in Req. Header
  138.         call    init_dev        ;INIT device
  139.         jnc    modify_mem
  140.         call    restore_xms        ;restore previous state of original XMS handler
  141.         lea    dx,err3
  142.         jmp    exit
  143. modify_mem:    
  144.         call    restore_xms        ;restore previous state of original XMS handler
  145.         test    flags,UMB_USED        ;Driver in other mem. block ?
  146.         jz    create_cb        ;No,skip
  147.         mov    bx,ending_offset
  148.         add    bx,15
  149.         rcr    bx,1
  150.         mov    cl,3
  151.         shr    bx,cl
  152.         add    bx,ending_segment
  153.         mov    ax,dev_seg
  154.         dec    ax            ;take into account ICB size!
  155.         sub    bx,ax            ;BX - size of mem. block in paragraphs
  156.         mov    es,ax            ;ES - seg. of the block
  157.         mov    ah,4ah            ;Modify alloc. mem. block
  158.         int    21h
  159.         jnc    create_cb
  160.         lea    dx,err7
  161.         jmp    exit_err
  162. create_cb:
  163.         mov    ax,dev_seg
  164.         mov    es,ax
  165.         mov    bx,drv_beg
  166.         call    create_icb        ;create Idrv Control Block                     
  167.         mov    ax,es:dev_attr[bx]
  168.         test    ah,80h            ;Character device ?
  169.         jnz    ldev            ;Yes,jump to link device headers
  170.         cmp    units,ZERO        ;Error during INIT ?
  171.         jnz    check_CDS        ;No
  172.         lea    dx,err3
  173.         jmp    exit            ;Yes,jump to quit
  174. check_CDS:
  175.         call    check_DA
  176.         jc    exit_err
  177. DIB_creation:        
  178.         call    seek_last        ;Seek the last DIB
  179.         mov    lastDIB_offs,bx
  180.         mov    lastDIB_seg,es
  181.  
  182.         test    flags,UMB_USED        ;driver in other mem.block ?
  183.         jz    DIB_after_drv        ;No,jump
  184.         mov    ax,our_seg
  185.         add    ax,10h            ;add PSP size
  186.         jmp    set_DIB_addr
  187.  
  188. DIB_after_drv:
  189.         mov    ax,ending_offset
  190.         add    ax,15
  191.         mov    cl,4
  192.         shr    ax,cl
  193.         add    ax,ending_segment
  194. set_DIB_addr:
  195.         mov    DIB_seg,ax
  196.         mov    DIB_offs,ZERO
  197.         
  198.         mov    cl,units        ;CX = number of units
  199.         xor    ch,ch
  200.         mov    dl,es:[bx]        ;DL = last drive#
  201.         mov    dh,0ffh
  202. DIB_continue:        
  203.         inc    dl            ;Set drive#
  204.         inc    dh            ;Set unit#
  205.         call    create_DIB        ;Create Disk Info Block
  206.         jnc    DA_creation
  207.         call    restore            ;Restore previous state of DIB & DA chains
  208.         jmp    exit_err
  209. DA_creation:
  210.         call    create_DA            ;Create Disk Area
  211.         les    bx,dword ptr DIB_offs
  212.         add    DIB_offs,DIB_SIZE            ;Set new DIB address
  213.         loop    DIB_continue
  214.                 
  215. ldev:        call    link_dev        ;Link device headers
  216.         jmp    exit_OK
  217. exit_err:
  218.         call    free_xmem
  219.         call    print_msg        
  220.  
  221. exit_OK:
  222.         mov    ax,our_seg
  223.         mov    es,ax
  224.         mov    es,es:2ch
  225.         mov    ah,49h        ; Free allocated memory (environment)
  226.         int    21h
  227.  
  228.         test    flags,UMB_USED    ; Driver in UMB ?
  229.         jz    drv_not_high    ; No,jump
  230.         mov    dx,100h        ; size of PSP
  231.         add    dx,ad_mem
  232.         add    dx,15
  233.         rcr    dx,1
  234.         mov    cl,3
  235.         shr    dx,cl
  236.         jmp    term_resident
  237.  
  238. drv_not_high:        
  239.         mov    dx,ending_offset    ;Compute address of ...
  240.         add    dx,ad_mem
  241.         add    dx,15
  242.         rcr    dx,1
  243.         mov    cl,3
  244.         shr    dx,cl
  245.  
  246.         add    dx,ending_segment
  247.         inc    dx            ; (add one para)
  248.         mov    ax,our_seg
  249.         sub    dx,ax            ; ... program end
  250. term_resident:
  251.         xor    al,al        ; set 0 for return code
  252.         mov    ah,31h        ;Terminate and remain resident
  253.         int    21h
  254.  
  255. exit:
  256.         call    print_msg
  257. exit_no_print:
  258.         mov    ah,4ch        ;EXIT
  259.         mov    al,1        ;Set return code
  260.         int    21h
  261. ;..............................................................................
  262. ; create_icb:
  263. ;    creates Idrv Control Block before Device Header
  264. ; Input:
  265. ;    ES:BX - Device Header address
  266. ;..............................................................................
  267. create_icb    proc    near
  268.  
  269.         push    es
  270.         mov    ax,es
  271.         dec    ax
  272.         mov    es,ax
  273.         mov    si,offset sign
  274.         mov    cx,SIG_LEN
  275.         mov    di,bx
  276.         cld
  277.         rep    movsb                ;place signature
  278.         mov    ax,handle
  279.         mov    word ptr es:xms_marker[bx],ax    ;the word will be ...
  280.                             ; ... 0 if handle is 0
  281.         cmp    ax,ZERO
  282.         je    xms_not_used
  283.         mov    word ptr es:xms_handle[bx],ax
  284.         mov    byte ptr es:xms_marker[bx],'X'    ;signal that XMS was used
  285.         mov    word ptr es:xms_marker[bx + 1],'SM'
  286.         mov    ax,prv_offs
  287.         mov    word ptr es:prv_xms_offs[bx],ax
  288.         mov    ax,prv_seg
  289.         mov    word ptr es:prv_xms_seg[bx],ax
  290. xms_not_used:
  291.         pop    es
  292.         ret
  293. create_icb    endp
  294.  
  295. page;..........................................................................
  296. ; link_dev:                                .......
  297. ;    links Device Header of the driver with the chain of             .......
  298. ;    Device Headers of the rest  drivers                .......                    .......
  299. ;..............................................................................
  300.  
  301.  
  302. link_dev:
  303.         mov    es,dos_seg
  304.         mov    si,48h
  305.         push    es
  306.         les    si,dword ptr es:[si]    ;Address of next dev. header
  307.         mov    bx,drv_beg
  308.         mov    ax,dev_seg
  309.         push    ds
  310.         mov    ds,ax
  311.         mov    next_offs[bx],si
  312.         mov    next_seg[bx],es
  313.         mov    bx,dev_attr[bx]        ;get dev.attribute for later
  314.         pop    ds
  315.  
  316.         pop    es
  317.         cli
  318.         mov    es:48h,word ptr ZERO
  319.         mov    ax,dev_seg
  320.         mov    es:4ah,ax            
  321.         sti
  322.     
  323.         test    bl,1            ;Is it Stdin device ?
  324.         jz    test_clk        ;No,jump
  325.         mov    es:32h,word ptr ZERO    ;Change pointer to ...
  326.         mov    es:34h,ax        ; ... Stdin
  327.         les    bx,es:02ah        ;Get address of DOS file table
  328.         add    bx,dos_dep        ;change entry length of OFT if necessary
  329.         mov    es:[bx + 042h],word ptr ZERO    ;Change address of dev.header ...
  330.         mov    es:[bx + 044h],ax        ; ... in Open Files Table
  331.         jmp    quit_link
  332.  
  333. test_clk:    test    bl,8            ;Is it clock device ?
  334.         jz    quit_link        ;No,jump
  335.         mov    es:2eh,word ptr ZERO
  336.         mov    es:30h,ax
  337.         
  338. quit_link:
  339.         ret        
  340.         
  341. ;..............................................................................
  342. ; load_drv:                                ....